<?php
/**
 * Course page controls LifterLMS Module
 *
 * @package LifterLMS Booster
 */

namespace Neve_Pro\Modules\LifterLMS_Booster\Customizer;

use Neve\Customizer\Base_Customizer;
use Neve\Customizer\Types\Control;
use Neve\Customizer\Types\Section;

/**
 * Class Course_Membership
 *
 * @package Neve_Pro\Modules\LifterLMS_Booster\Customizer
 */
class Course_Membership extends Base_Customizer {

	/**
	 * Add customizer controls
	 */
	public function add_controls() {
		$this->add_course_membership_section();
		$this->add_course_layout_controls();
		$this->add_membership_layout_controls();
		$this->add_colors();
	}

	/**
	 * Add Course/Membership section.
	 */
	private function add_course_membership_section() {
		$this->add_section(
			new Section(
				'neve_liftellms',
				array(
					'priority' => 85,
					'title'    => esc_html__( 'LifterLMS', 'neve' ),
					'panel'    => 'neve_layout',
				)
			)
		);
	}

	/**
	 * Add course customizer controls.
	 */
	private function add_course_layout_controls() {

		$this->add_control(
			new Control(
				'neve_course_page_ui_heading',
				array(
					'sanitize_callback' => 'sanitize_text_field',
					'transport'         => $this->selective_refresh,
				),
				array(
					'label'            => esc_html__( 'Courses Shop', 'neve' ),
					'section'          => 'neve_liftellms',
					'priority'         => 10,
					'class'            => 'course-page-layout',
					'accordion'        => true,
					'controls_to_wrap' => 4,
					'expanded'         => true,
				),
				'Neve\Customizer\Controls\Heading'
			)
		);

		$this->add_control(
			new Control(
				'neve_courses_per_row',
				array(
					'sanitize_callback' => 'neve_sanitize_range_value',
					'default'           => json_encode(
						array(
							'desktop' => 3,
							'tablet'  => 2,
							'mobile'  => 1,
						)
					),
				),
				array(
					'label'      => esc_html__( 'Courses per row', 'neve' ),
					'section'    => 'neve_liftellms',
					'units'      => array(
						'items',
					),
					'input_attr' => array(
						'mobile'  => array(
							'min'     => 1,
							'max'     => 6,
							'default' => 1,
						),
						'tablet'  => array(
							'min'     => 1,
							'max'     => 6,
							'default' => 2,
						),
						'desktop' => array(
							'min'     => 1,
							'max'     => 6,
							'default' => 3,
						),
					),
					'priority'   => 15,
					'responsive' => true,
				),
				'Neve\Customizer\Controls\Responsive_Number'
			)
		);

		$this->add_control(
			new Control(
				'neve_course_pagination_type',
				array(
					'default'           => 'number',
					'sanitize_callback' => array( $this, 'sanitize_pagination_type' ),
				),
				array(
					'label'    => esc_html__( 'Pagination', 'neve' ),
					'section'  => 'neve_liftellms',
					'priority' => 20,
					'type'     => 'select',
					'choices'  => array(
						'number'   => esc_html__( 'Number', 'neve' ),
						'infinite' => esc_html__( 'Infinite Scroll', 'neve' ),
					),
				)
			)
		);

		$this->add_control(
			new Control(
				'neve_course_box_shadow_intensity',
				array(
					'sanitize_callback' => 'absint',
					'default'           => 0,
				),
				array(
					'label'      => esc_html__( 'Card Box shadow (px)', 'neve' ),
					'section'    => 'neve_liftellms',
					'type'       => 'range-value',
					'step'       => 1,
					'input_attr' => array(
						'min'     => 0,
						'max'     => 30,
						'default' => 0,
					),
					'priority'   => 30,
				),
				'Neve\Customizer\Controls\Range'
			)
		);

		$this->add_control(
			new Control(
				'neve_course_card_layout',
				array(
					'default'           => 'grid',
					'sanitize_callback' => array( $this, 'sanitize_card_layout' ),
				),
				array(
					'label'    => esc_html__( 'Card Layout', 'neve' ),
					'section'  => 'neve_liftellms',
					'priority' => 35,
					'choices'  => array(
						'grid' => array(
							'url' => '',
						),
						'list' => array(
							'url' => '',
						),
					),
				),
				'Neve\Customizer\Controls\Radio_Image'
			)
		);
	}

	/**
	 * Add membership customizer controls.
	 */
	private function add_membership_layout_controls() {

		$this->add_control(
			new Control(
				'neve_membership_page_ui_heading',
				array(
					'sanitize_callback' => 'sanitize_text_field',
					'transport'         => $this->selective_refresh,
				),
				array(
					'label'            => esc_html__( 'Memberships Shop', 'neve' ),
					'section'          => 'neve_liftellms',
					'priority'         => 100,
					'class'            => 'membership-page-layout',
					'accordion'        => true,
					'controls_to_wrap' => 4,
					'expanded'         => false,
				),
				'Neve\Customizer\Controls\Heading'
			)
		);

		$this->add_control(
			new Control(
				'neve_memberships_per_row',
				array(
					'sanitize_callback' => 'neve_sanitize_range_value',
					'default'           => json_encode(
						array(
							'desktop' => 3,
							'tablet'  => 2,
							'mobile'  => 1,
						)
					),
				),
				array(
					'label'      => esc_html__( 'Memberships per row', 'neve' ),
					'section'    => 'neve_liftellms',
					'units'      => array(
						'items',
					),
					'input_attr' => array(
						'mobile'  => array(
							'min'     => 1,
							'max'     => 6,
							'default' => 1,
						),
						'tablet'  => array(
							'min'     => 1,
							'max'     => 6,
							'default' => 2,
						),
						'desktop' => array(
							'min'     => 1,
							'max'     => 6,
							'default' => 3,
						),
					),
					'priority'   => 110,
					'responsive' => true,
				),
				'Neve\Customizer\Controls\Responsive_Number'
			)
		);

		$this->add_control(
			new Control(
				'neve_membership_pagination_type',
				array(
					'default'           => 'number',
					'sanitize_callback' => array( $this, 'sanitize_pagination_type' ),
				),
				array(
					'label'    => esc_html__( 'Pagination', 'neve' ),
					'section'  => 'neve_liftellms',
					'priority' => 115,
					'type'     => 'select',
					'choices'  => array(
						'number'   => esc_html__( 'Number', 'neve' ),
						'infinite' => esc_html__( 'Infinite Scroll', 'neve' ),
					),
				)
			)
		);

		$this->add_control(
			new Control(
				'neve_membership_box_shadow_intensity',
				array(
					'sanitize_callback' => 'absint',
					'default'           => 0,
				),
				array(
					'label'      => esc_html__( 'Card Box shadow (px)', 'neve' ),
					'section'    => 'neve_liftellms',
					'type'       => 'range-value',
					'step'       => 1,
					'input_attr' => array(
						'min'     => 0,
						'max'     => 30,
						'default' => 0,
					),
					'priority'   => 125,
				),
				'Neve\Customizer\Controls\Range'
			)
		);

		$this->add_control(
			new Control(
				'neve_membership_card_layout',
				array(
					'default'           => 'grid',
					'sanitize_callback' => array( $this, 'sanitize_card_layout' ),
				),
				array(
					'label'    => esc_html__( 'Card Layout', 'neve' ),
					'section'  => 'neve_liftellms',
					'priority' => 130,
					'choices'  => array(
						'grid' => array(
							'url' => '',
						),
						'list' => array(
							'url' => '',
						),
					),
				),
				'Neve\Customizer\Controls\Radio_Image'
			)
		);
	}

	/**
	 * Add colors for LifterLMS.
	 */
	private function add_colors() {

		$this->add_control(
			new Control(
				'neve_lifter_colors_ui_heading',
				array(
					'sanitize_callback' => 'sanitize_text_field',
					'transport'         => $this->selective_refresh,
				),
				array(
					'label'            => esc_html__( 'Colors', 'neve' ),
					'section'          => 'neve_liftellms',
					'priority'         => 200,
					'class'            => 'lifter-colors',
					'accordion'        => true,
					'controls_to_wrap' => 1,
					'expanded'         => false,
				),
				'Neve\Customizer\Controls\Heading'
			)
		);

		$this->add_control(
			new Control(
				'neve_lifter_primary_color',
				array(
					'sanitize_callback' => 'neve_sanitize_colors',
					'default'           => '#2295ff',
				),
				array(
					'label'    => 'Primary Color',
					'section'  => 'neve_liftellms',
					'priority' => 205,
				),
				'WP_Customize_Color_Control'
			)
		);

	}

	/**
	 * Sanitize the pagination type
	 *
	 * @param string $value value from the control.
	 *
	 * @return bool
	 */
	public function sanitize_pagination_type( $value ) {
		$allowed_values = array( 'number', 'infinite' );
		if ( ! in_array( $value, $allowed_values, true ) ) {
			return 'number';
		}

		return esc_html( $value );
	}

	/**
	 * Sanitize card layout value.
	 *
	 * @param string $value Value from the control.
	 *
	 * @return bool
	 */
	public function sanitize_card_layout( $value ) {
		$allowed_values = array( 'list', 'grid' );
		if ( ! in_array( $value, $allowed_values, true ) ) {
			return 'grid';
		}

		return esc_html( $value );
	}
}
